package com.tanhua.sso;

import com.tanhua.common.pojo.Person;

import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MongodbApplication {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void testSave(){
        //数据
        Person p=new Person();
        p.setName("admin2");
        p.setAge(20);
        mongoTemplate.save(p);
    }

    @Test
    public void testDelete(){
        Query query=Query.query(Criteria.where("id").is(new ObjectId("612f5b7878960f2d0c4ca7a9")));
        mongoTemplate.remove(query,Person.class);
    }

    @Test
    public void testQueryForPage(){

        //page:当前页码 从0开始  当前页码-1  size:每页显示条目数
        PageRequest pageRequest=new PageRequest(1,2);
        Query query=new Query().with(pageRequest);
        List<Person> list = mongoTemplate.find(query, Person.class);
        System.out.println(list);
    }
}
